'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)

	let baseUrl = 'http://aliv18.data.moji.com';
	let apiUrl = baseUrl + '/whapi/json/alicityweather/forecast15days';
	const res = await uniCloud.httpclient.request(apiUrl, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
			'Authorization': 'APPCODE 277ec4ab3a3747dcbd6de6f4687e5fb5'
		},
		data: {
			cityId: event.cityId,
			token: "f9f212e1996e79e0e602b08ea297ffb0"
		},
		contentType: 'json', // 指定以application/json发送data内的数据
		dataType: 'json' // 指定返回值为json格式，自动进行parse
	})
	console.log("getWeatherHistory res is: ", res);

	for (let item of res.data.data.forecast) {
		console.log("for of is: ", item);
		let data = await db.collection("weather").where({
			city_id: item.cityId,
			predict_date: item.predictDate
		}).get();
		console.log("weather get: ", data);
		let result;
		if (data.data.length > 0) {
			result = await db.collection("weather").doc(data.data[0]._id).set({
				condition_day: item.conditionDay,
				condition_id_day: item.conditionIdDay,
				condition_id_night: item.conditionIdNight,
				condition_night: item.conditionNight,
				humidity: item.humidity,
				moonphase: item.moonphase,
				moonrise: item.moonrise,
				moonset: item.moonset,
				pop: item.pop,
				predict_date: item.predictDate,
				qpf: item.qpf,
				sunrise: item.sunrise,
				sunset: item.sunset,
				temp_day: item.tempDay,
				temp_night: item.tempNight,
				updatetime: item.updatetime,
				uvi: item.uvi,
				wind_degrees_day: item.windDegreesDay,
				wind_degrees_night: item.windDegreesNight,
				wind_dir_day: item.windDirDay,
				wind_dir_night: item.windDirNight,
				wind_level_day: item.windLevelDay,
				wind_level_night: item.windLevelNight,
				wind_speed_day: item.windSpeedDay,
				wind_speed_night: item.windSpeedNight,
				city_id: res.data.data.city.cityId,
				city_name: item.predictDate
			})
			console.log("weather update: ", result);
		} else {
			result = await db.collection("weather").add({
				condition_day: item.conditionDay,
				condition_id_day: item.conditionIdDay,
				condition_id_night: item.conditionIdNight,
				condition_night: item.conditionNight,
				humidity: item.humidity,
				moonphase: item.moonphase,
				moonrise: item.moonrise,
				moonset: item.moonset,
				pop: item.pop,
				predict_date: item.predictDate,
				qpf: item.qpf,
				sunrise: item.sunrise,
				sunset: item.sunset,
				temp_day: item.tempDay,
				temp_night: item.tempNight,
				updatetime: item.updatetime,
				uvi: item.uvi,
				wind_degrees_day: item.windDegreesDay,
				wind_degrees_night: item.windDegreesNight,
				wind_dir_day: item.windDirDay,
				wind_dir_night: item.windDirNight,
				wind_level_day: item.windLevelDay,
				wind_level_night: item.windLevelNight,
				wind_speed_day: item.windSpeedDay,
				wind_speed_night: item.windSpeedNight,
				city_id: res.data.data.city.cityId,
				city_name: item.predictDate
			})
			console.log("weather add: ", result);
		}

	}

	//返回数据给客户端
	return res.data;
}